package edit;

import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import pulpcore.sprite.Sprite;
import pulpcore.Input;
import draw.DrawCircleSprite;
import pulpfizz.physics.BodyUtils;
import pulpfizz.physics.Material;
import pulpfizz.pulp.body.Java2DBodySprite;

import java.awt.*;

import goliath.BasicDemo;

/**
 * Circle draw: Initial click for center, drag for radius
 * @author will
 * @date Aug 9, 2008 9:46:15 PM
 */
public class DrawCircleContext extends EditContext {
    Vec2 startPos;
    Sprite drawSprite;

    boolean pressed;

    public DrawCircleContext() {
        super();
        startPos = new Vec2();
    }

    public void update(int elapsedTime) {
        if(Input.isMousePressed()) {
            // Record the starting position
            startPos.set(mousePos);

            drawSprite = new DrawCircleSprite(this);
            layer.add(drawSprite);
            pressed = true;
        }

        if(Input.isMouseReleased() && pressed) {
            pressed = false;
            Vec2 start = startPos.add(mousePos).mul(0.5f);
            float dim = startPos.sub(mousePos).length();
            Body myCircle = BodyUtils.createCircle(layer.getWorld(),start.x,start.y,dim);
            if(!BasicDemo.isFixed()) {
                Material.DEAD_WEIGHT().applyAll(myCircle);
            }
            Java2DBodySprite bs = new Java2DBodySprite(myCircle,layer,2, BasicDemo.getDrawColor(), BasicDemo.getFillColor());
            layer.add(bs);

            layer.remove(drawSprite);
        }

    }

    public void reset() {
        startPos.setZero();
        drawSprite = null;
        pressed = false;
    }

    public String toString() {
        return "Circle";
    }


}
